/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import spagcore.SpaglCoreList;

/**
 * A JList (javax.swing.JList) that conforms to the SpaglCoreList interface
 * @author Sam Pottinger
 */
public class SpaglList extends JList implements SpaglCoreList{

	private static final long serialVersionUID = 1L;
	private DefaultListModel model;
	private SpaglCoreContainer parentContainer;
	private SpaglCoreFont widgetFont;
	private JScrollPane scrollPane;
	
	public static final int SINGLE = ListSelectionModel.SINGLE_SELECTION;
	public static final int MULTI = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;

	/**
	 * Creates a list with the default layout behavior
	 * @param targetContainer The container that will hold this widget
	 * @param newSelectionMode The selection mode to be used by this list (either the int values of the class variables SINGLE or MULTI)
	 */
	public SpaglList(SpaglCoreContainer targetContainer, int newSelectionMode)
	{
		super(new DefaultListModel());
		scrollPane = new JScrollPane(this);
		model = (DefaultListModel)super.getModel();
		parentContainer = targetContainer;
	}
	
	/**
	 * Creates a list with specified parameters for the layout manager
	 * @param targetContainer The container that will hold this widget
	 * @param newSelectionMode The selection mode to be used by this list (either the int values of the class variables SINGLE or MULTI)
	 * @param parameters The string parameters to be used for the layout manager
	 */
	public SpaglList(SpaglCoreContainer targetContainer, int newSelectionMode, String parameters)
	{
		super(new DefaultListModel());
		scrollPane = new JScrollPane(this);
		model = (DefaultListModel)super.getModel();
		parentContainer = targetContainer;
		super.setSelectionMode(newSelectionMode);
		((SpaglWindow)parentContainer).placeWidget((Component)scrollPane, parameters);
	}
	
	/**
	 * Adds an object to this list as an option
	 * @param newElement The new object to be added
	 */
	public void addElement(Object newElement) {
		model.addElement(newElement);
	}

	/**
	 * Adds an object to this list as an option at a given position
	 * @param position The integer position where the object will be inserted
	 * @param newElement The new object to be added
	 */
	public void addElement(int position, Object newElement) {
		model.add(position, newElement);
	}

	/**
	 * Gets the indicies currently selected in this list
	 * @return An array of indicies currently highlighted in this widget
	 */
	public int[] getSelectedIndicies() {
		return super.getSelectedIndices();
	}

	/**
	 * Completely clears all selection options from this list
	 */
	public void removeAllElements() {
		model.removeAllElements();
	}

	/**
	 * Removes the object at a given index from this list
	 * @param i The index of the option to be removed
	 */
	public void removeElement(int i) {
		model.remove(i);
	}

	/**
	 * Sets the list of options available
	 * @param newElementList A list of objects to be used as selection options
	 */
	public void setElementList(Object[] newElementList) {
		this.removeAllElements();
		for(Object o : newElementList)
			this.addElement(o);
	}

	/**
	 * Sets the options to be highlighted as selected in this list
	 * @param i The indicies of the objects to be highlighted as selected
	 */
	public void setSelectedIndicies(int[] i) {
		super.setSelectedIndices(i);
	}

	/**
	 * Gets the parent container that is holding this widget
	 * @return This object's parent container as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentContainer;
	}

	/**
	 * Gets the font currently used by this widget
	 * @return The SpaglFont used by this widget as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 */
	public void setFont(SpaglCoreFont newFont) {
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Sets the font to be used by this widget
	 * @param newFont The {@link SpaglCoreFont}-compatable font used by this widget
	 * @param dispose True if the font should be disposed of immediately (experimental)
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		if(!dispose)
			widgetFont = newFont;
		else
			widgetFont = null;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}

	/**
	 * Adds an event listener to this list
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {this.addListSelectionListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.SELECTION_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}

}
